/*
 * Package: org.grassfield.common.view
 * FileName: HelloWorld.java
 *  
 * Created by pandian on Jan 28, 2011
 */
package action;

import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

import service.IUserService;

import bean.User;

import com.opensymphony.xwork2.ActionSupport;


/**
 * The Class HelloWorld.
 */
public class HelloWorld extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private IUserService userService;
	private Map<String, Object> session = null;
	private String userName;
	private String password;
	private String message;
	

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	/**
	 * Gets the message.
	 * 
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * Sets the message.
	 * 
	 * @param message
	 *            the new message
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	
	/**
	 * Gets the user name.
	 * 
	 * @return the user name
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * Sets the user name.
	 * 
	 * @param userName
	 *            the new user name
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * Gets the password.
	 * 
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Sets the password.
	 * 
	 * @param password
	 *            the new password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() {
		User user =(User) this.session.get("user$");
		//if user not found, login now
		if (user == null) {

			if (this.userName == null) {
				super.addFieldError("userName", "User name is null");
				return "login-error";
			}
			if (this.userName.trim().length() == 0) {
				super.addFieldError("userName", "User name is empty");
				return "login-error";
			}
			if (this.password == null) {
				super.addFieldError("userName", "Password is null");
				return "login-error";
			}
			if (this.password.length() == 0) {
				super.addFieldError("userName", "Password is empty");
				return "login-error";
			}

			user = this.userService.getUser(userName, password);
			if (user == null) {
				super.addActionError("Validation failed.");
				return "login-error";
			}
			this.session.put("user$", user);
		}
		System.out.println("email:"+user.getContactInfo().getEmail1());
        return "SUCCESS";
    }

	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
	 */
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}
}
